package com.enjoyor.soa.traffic.core.efoms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.loader.custom.Return;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.efoms.domain.CheckRecodeInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.DeviceStatusInfo;
import com.enjoyor.soa.traffic.core.efoms.mapper.ICheckRecodeInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.service.ICheckRecordInfoService;
import com.enjoyor.soa.traffic.core.efoms.service.IDevStatusInfoService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

@Service
@Transactional
@SuppressWarnings("rawtypes")
public class CheckRecordInfoService implements ICheckRecordInfoService{
	
	@Autowired
	private ICheckRecodeInfoMapper checkRecodeInfoMapper;
	
	@Autowired
	private IDevStatusInfoService devStatusInfoService;
	
	@Autowired
	private IPubLogMapper pubLogMapper;
	

	@Override
	public CheckRecodeInfo getCheckRecordInfo(String CheckRecordInfoId) {
		// TODO Auto-generated method stub
		try {
			return checkRecodeInfoMapper.selectById(CheckRecordInfoId);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public List<CheckRecodeInfo> getCheckRecodeInfoList(String CheckRecordInfoId,String devId,String checkPersonCode,String startTime,String endTime,String devAreaCode,String devAreaName,String checkPersonName,String devCategoryCode,String devTypeCode,String checkResultCode) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> paramT = this.getPraTMap(CheckRecordInfoId,devId,checkPersonCode,startTime,endTime,devAreaCode,devAreaName,checkPersonName,devCategoryCode,devTypeCode,checkResultCode,null);
			return checkRecodeInfoMapper.getCheckRecordInfoList(paramT);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public Page getCheckRecordInfoPage(String CheckRecordInfoId,String devId,String checkPersonCode,String startTime,String endTime,String devAreaCode,String devAreaName,String checkPersonName,String devCategoryCode,String devTypeCode,String checkResultCode,String devName,Page page) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> paramT = this.getPraTMap(CheckRecordInfoId,devId,checkPersonCode,startTime,endTime,devAreaCode,devAreaName,checkPersonName,devCategoryCode,devTypeCode,checkResultCode,devName);
			page.setMapBean(paramT);
			return checkRecodeInfoMapper.getCheckRecordInfoPage(page);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public ResultPojo insertCheckRecordInfo(List<CheckRecodeInfo> CheckRecodeInfoList,String userName) {
		// TODO Auto-generated method stub
		try {
			for(int i = 0;i<CheckRecodeInfoList.size();i++){
				CheckRecodeInfo insertCheckRecodeInfo = this.getCheckRecordInfo(CheckRecodeInfoList.get(i));
				this.checkRecodeInfoMapper.insertSelective(insertCheckRecodeInfo);
				DeviceStatusInfo deviceStatusInfo = this.getDeviceStatusInfo(insertCheckRecodeInfo);
				this.devStatusInfoService.insertDevStatusInfo(deviceStatusInfo, userName);
				pubLogMapper.saveNoramlLogInfo(userName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
						String.format("添加巡检记录编号:{%s} 操作成功!", new Object[] {CheckRecodeInfoList.get(i).getCheckRecodeId()}),
						LogInfoDic.LOG_MODULAR_ENUM.EFOMS_CHECK_MANAGEMENT.getMessage());
			}
			return ResultHelper.getResult("成功！");
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public ResultPojo updateCheckRecordInfo(CheckRecodeInfo checkRecodeInfo,String userName) {
		// TODO Auto-generated method stub
		try {
			this.checkRecodeInfoMapper.updateByIdSelective(checkRecodeInfo);
			pubLogMapper.saveNoramlLogInfo(userName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
					String.format("修改巡检记录编号:{%s} 操作成功!", new Object[] {checkRecodeInfo.getCheckRecodeId()}),
					LogInfoDic.LOG_MODULAR_ENUM.EFOMS_CHECK_MANAGEMENT.getMessage());
			return ResultHelper.getResult(checkRecodeInfo.getCheckRecodeId());			
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public ResultPojo delCheckRecordInfo(String CheckRecordInfoId,String userName) {
		// TODO Auto-generated method stub
		try {
			this.checkRecodeInfoMapper.deleteById(CheckRecordInfoId);
			pubLogMapper.saveNoramlLogInfo(userName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
					String.format("删除巡检记录编号:{%s} 操作成功!", new Object[] {CheckRecordInfoId}),
					LogInfoDic.LOG_MODULAR_ENUM.EFOMS_CHECK_MANAGEMENT.getMessage());
			return ResultHelper.getResult(CheckRecordInfoId);			
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}
	
	//----------------------内部方法开始-------------------------------------//
	
	/**
	 * 获取查询map条件；
	 * @param CheckRecordInfoId 
	 * @return
	 */
	private Map<String, Object> getPraTMap(String CheckRecordInfoId,String devId,String checkPersonCode,String startTime,String endTime,String devAreaCode,String devAreaName,String checkPersonName,String devCategoryCode,String devTypeCode,String checkResultCode,String devName) {
		Map<String, Object> paramT = new HashMap<>();
		paramT.put("checkRecodeId", CheckRecordInfoId);
		paramT.put("devId", devId);
		paramT.put("checkPersonCode", checkPersonCode);
		paramT.put("startTime", startTime);
		paramT.put("endTime", endTime);
		paramT.put("devAreaCode", devAreaCode);
		paramT.put("devAreaName", devAreaName);
		paramT.put("checkPersonName", checkPersonName);
		paramT.put("devCategoryCode", devCategoryCode);
		paramT.put("devTypeCode", devTypeCode);
		paramT.put("checkResultCode", checkResultCode);
		paramT.put("devName", devName);
		return paramT;
	}
	
	/**
	 * 获取添加对象；
	 * @param checkRecodeInfo 
	 * @return
	 */
	private CheckRecodeInfo getCheckRecordInfo(CheckRecodeInfo checkRecodeInfo){
		String checkRecordId = "C" + TimeHelper.getUniqueString();
		String checkTime = TimeHelper.dateToLongString();
		checkRecodeInfo.setCheckTime(checkTime);
		checkRecodeInfo.setCheckRecodeId(checkRecordId);
		return checkRecodeInfo;
	}
	
	/**
	 * 获取设备状态对象信息；
	 * @param checkStatusInfo 
	 * @return
	 */
	private DeviceStatusInfo getDeviceStatusInfo(CheckRecodeInfo checkRecodeInfo){
		DeviceStatusInfo deviceStatusInfo = new DeviceStatusInfo();
		deviceStatusInfo.setCheckResultCode(checkRecodeInfo.getCheckResultCode());
		deviceStatusInfo.setCheckResultName(checkRecodeInfo.getCheckResultName());
		deviceStatusInfo.setDevCategoryCode(checkRecodeInfo.getDevCategoryCode());
		deviceStatusInfo.setDevCategoryName(checkRecodeInfo.getDevCategoryName());
		deviceStatusInfo.setDevId(checkRecodeInfo.getDevId());
		deviceStatusInfo.setDevName(checkRecodeInfo.getDevName());
		deviceStatusInfo.setDevTypeCode(checkRecodeInfo.getDevTypeCode());
		deviceStatusInfo.setDevTypeName(checkRecodeInfo.getDevTypeName());
		deviceStatusInfo.setLastCheckTime(TimeHelper.dateToLongString());
		return deviceStatusInfo;
	}
		
}
